// const { think } = require('thinkjs');
// const moment = require('moment');
const { think } = require('thinkjs');
const Base = require('./base.js');
const cid = require('@4a/cid');
const { default: BigNumber } = require('bignumber.js');

module.exports = class extends Base {
  async getCartAction() {
    if (think.isEmpty(this.get('ids'))) {
      const data = await this.model('cart_list').alias('a').join({
        product_list: {
          join: 'left',
          as: 'd',
          on: ['product_id', 'id']
        }
      }).field('d.name, d.price, a.*').where({user_id: this.getLoginUserId()}).order(['id DESC']).select();
      return this.success(data);
    } else {
      const data = await this.model('cart_list').alias('a').join({
        product_list: {
          join: 'left',
          as: 'd',
          on: ['product_id', 'id']
        }
      }).field('d.name, d.price, a.*').where({user_id: this.getLoginUserId(), 'a.id': ['IN', this.get('ids')]}).order(['id DESC']).select();
      return this.success(data);
    }
  }

  async addCartAction() {
    const id = this.post('id');
    const area = this.post('area');
    const tag = this.post('tag') || '';
    // const has = await
    await this.model('cart_list').add({
      create_time: ['exp', 'CURRENT_TIMESTAMP()'],
      product_id: id,
      area: area,
      tag: tag,
      user_id: this.getLoginUserId()
    });
    return this.success();
  }

  async delCartAction() {
    await this.model('cart_list').where({id: ['IN', this.get('ids')], user_id: this.getLoginUserId()}).delete();

    const data = await this.model('cart_list').alias('a').join({
      product_list: {
        join: 'left',
        as: 'd',
        on: ['product_id', 'id']
      }
    }).field('d.name, d.price, a.*').where({user_id: this.getLoginUserId()}).order(['id DESC']).select();
    return this.success(data);
  }

  async getOrderAction() {
    const orders = await this.model('order_list').where({order_no: this.get('no'), user_id: this.getLoginUserId()}).alias('a').join({
      product_list: {
        join: 'left',
        as: 'd',
        on: ['product_id', 'id']
      }
    }).field('d.name, d.price, a.*').select();
    let money = 0;
    for (const item of orders) {
      money = BigNumber(money).plus(BigNumber(item.price));
    }
    // const money = BigNumber().plus(BigNumber());

    return this.success({
      order_no: this.get('no'),
      money: money
    });
  }

  async submitCartAction() {
    const ids = this.post('ids');
    if (think.isEmpty(ids)) {
      return this.fail(408, '找不到订单');
    }

    const carts = await this.model('cart_list').where(({id: ['IN', ids]})).select();
    if (think.isEmpty(carts)) {
      return this.fail(408, '找不到订单');
    }

    const orderSn = cid(8, { prefix: 'ZS' });

    for (const item of carts) {
      const product = await this.model('product_list').where({id: item.product_id}).find();
      await this.model('order_list').add({
        user_id: this.getLoginUserId(),
        product_id: item.product_id,
        area: item.area,
        tag: item.tag,
        price: product.price,
        create_time: ['exp', 'CURRENT_TIMESTAMP()'],
        order_no: orderSn
      });
      await this.model('cart_list').where({id: item.id}).limit(1).delete();
    }

    return this.success(orderSn);
  }

  async getPayAction() {
    const orders = await this.model('order_list').where({order_no: this.get('no'), user_id: this.getLoginUserId()}).select();
    return this.success(orders);
  }

  async commetAction() {
    const data = await this.model('order_comment').alias('a').join({
      user_list: {
        join: 'left',
        as: 'u',
        on: ['user_id', 'id']
      },
      product_list: {
        join: 'left',
        as: 'p',
        on: ['product_id', 'id']
      }
    }).field('u.name, u.phone, p.name as pName, a.*').order(['id DESC']).where({product_id: this.get('id')}).select();
    return this.success(data);
  }
};
